//
//  LaunchVC.m
//  ImGuider
//
//  Created by llt on 2017/4/10.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "LaunchVC.h"
#import "MainViewController.h"
#import <LGSideMenuController.h>
#import "MainRecommendVC.h"

#import "AppIntroduceVC.h"
#import "WGS84TOGCJ02.h"

@interface LaunchVC ()

//@property (nonatomic, strong) KKSequenceImageView *giftImageView;

@property (nonatomic) BOOL annimationHasLoad;
@property (nonatomic) BOOL dataHasLoad;
@property (nonatomic, strong) City *city;
@property (nonatomic, strong) City *locationcity;

//@property (nonatomic, strong) LOTAnimationView *lottieLogo;

@property (weak, nonatomic) IBOutlet UIImageView *mainBG;

@property (weak, nonatomic) IBOutlet UIImageView *logoBG;
@property (weak, nonatomic) IBOutlet UIImageView *logo2BG;
@property (weak, nonatomic) IBOutlet UILabel *sloganLabel;

@property(nonatomic,strong) NSTimer *rollTimer;//滚动视图计时器

@property (nonatomic) CGFloat lastScale;

@end

@implementation LaunchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupData];
}

- (void)setupUI {
    
    self.sloganLabel.text = MYLocalizedString(@"LAUNCH_INVALIDNAME", nil);
    self.logo2BG.image = [UIImage imageNamed:MYLocalizedString(@"LAUNCH_OPEN", nil)];
    
    self.lastScale = 1.0;
    
    self.rollTimer =[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(scaleImageAction) userInfo:nil repeats:YES];
    [self.rollTimer fire];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.annimationHasLoad = YES;
        if (self.dataHasLoad) {
            [self gotoMain];
        }
    });
    
    self.mainBG.alpha = 0;
    @weakify(self);
    [UIView animateWithDuration:2.0f animations:^{
        @strongify(self);
        self.mainBG.alpha = 1;
    }];
}

- (void)scaleImageAction {
    
    self.mainBG.transform = CGAffineTransformMakeScale(self.lastScale, self.lastScale);
    self.lastScale += 0.0004;
    
    if (self.lastScale > 2) {
        self.lastScale = 2;
        [self.rollTimer invalidate];
        self.rollTimer = nil;
    }
}

- (void)setupData {
    
    NSNumber *lastCityID = [AppManager sharedManager].lastCityID;
    
    if (!lastCityID) {
        
        self.dataHasLoad = YES;
        
        if (self.annimationHasLoad) {
            [self gotoMain];
        }
        
        return;
    }
    
    @weakify(self);
    [NetAPIManager getRecommendScenicWithCityID:lastCityID andBlock:^(City *city, NSError *error) {
        @strongify(self);
        
        self.city = city;
        
        self.dataHasLoad = YES;
        
        if (self.annimationHasLoad) {
            [self gotoMain];
        }
    }];
    
    //    @weakify(self);
    //    [LocationManager sharedManager].onceLocation = ^(CLLocation *location, NSError *error) {
    //        @strongify(self);
    //
    //        if (error) {
    //
    //            }
    //
    //            self.dataHasLoad = YES;
    //
    //            if (self.annimationHasLoad) {
    //                [self goRootVC];
    //            }
    //
    //            return;
    //        }
    //
    //        CLLocationCoordinate2D coor = transformFromGCJToWGS(location.coordinate, nil);
    //
    //        [NetAPIManager getRecommendScenicWithLocation:coor andBlock:^(City *locationcity, NSError *error) {
    //
    //            NSNumber *lastCityID = [AppManager sharedManager].lastCityID;
    //
    //            if (locationcity && (!lastCityID || [lastCityID isEqualToNumber:locationcity.cid])) {
    //
    //                self.city = locationcity;
    //                self.dataHasLoad = YES;
    //
    //                if (self.annimationHasLoad) {
    //                    [self goRootVC];
    //                }
    //                [AppManager sharedManager].lastCityID = locationcity.cid;
    //                return ;
    //            }
    //
    //            if (lastCityID) {
    //
    //                [NetAPIManager getRecommendScenicWithCityID:lastCityID andBlock:^(City *city, NSError *error) {
    //
    //                    self.city = city;
    //                    self.locationcity = locationcity;
    //                    self.dataHasLoad = YES;
    //
    //                    if (self.annimationHasLoad) {
    //                        [self goRootVC];
    //                    }
    //                }];
    //                return;
    //            }
    
    //            self.dataHasLoad = YES;
    //
    //            if (self.annimationHasLoad) {
    //                [self goRootVC];
    //            }
    //
    //        }];
    //    };
}


- (void)gotoMain {
    
    [_rollTimer invalidate];
    _rollTimer = nil;
    
    NSNumber *lastID = [AppManager sharedManager].lastCityID;
    
    if (!lastID) {
        
        AppIntroduceVC * appIntroduceVC = [[AppIntroduceVC alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = appIntroduceVC;
        return;
    }
    
    UIViewController *leftVC = [IGUtils loadViewController:@"LeftVC" storyboard:@"Account"];
    
    MainViewController *mainVC = [MainViewController new];
    
    [AppManager sharedManager].manVC = mainVC;
    mainVC.city = self.city;
    
    BaseNavigationController *navigationController = [[BaseNavigationController alloc] initWithRootViewController:mainVC];
    
    LGSideMenuController *sideMenuController = [LGSideMenuController sideMenuControllerWithRootViewController:navigationController leftViewController:leftVC rightViewController:nil];
    
    sideMenuController.leftViewWidth = SCREEN_WIDTH * 4 / 5.0;
    sideMenuController.leftViewPresentationStyle = LGSideMenuPresentationStyleSlideAbove;
    
    [UIApplication sharedApplication].keyWindow.rootViewController = sideMenuController;
}

@end

